package com.sora.pipeline.controller;

import com.sora.pipeline.common.api.ApiResponse;
import com.sora.pipeline.domain.User;
import com.sora.pipeline.domain.dto.*;
import com.sora.pipeline.service.AuthService;
import jakarta.validation.Valid;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/auth")
public class AuthController {

    private final AuthService authService;

    public AuthController(AuthService authService) {
        this.authService = authService;
    }

    // 1. 登录
    @PostMapping("/login")
    public ApiResponse<Map<String, Object>> login(@Valid @RequestBody LoginRequest req) {
        Map<String, Object> result = authService.login(req.getUsername(), req.getPassword(), req.getCaptcha());
        if ((int)result.getOrDefault("code", 0) != 0) {
            return ApiResponse.failure((int)result.get("code"), (String)result.get("message"));
        }
        Map<String, Object> data = new HashMap<>();
        data.put("token", result.get("token"));
        data.put("userInfo", result.get("userInfo"));
        data.put("expiresIn", result.get("expiresIn"));
        return ApiResponse.success(data);
    }

    // 2. 注册
    @PostMapping("/register")
    public ApiResponse<Map<String, Object>> register(@Valid @RequestBody RegisterRequest req) {
        Map<String, Object> result = authService.register(req);
        if ((int)result.getOrDefault("code", 0) != 0) {
            return ApiResponse.failure((int)result.get("code"), (String)result.get("message"));
        }
        Map<String, Object> data = new HashMap<>();
        data.put("registrationId", result.get("registrationId"));
        data.put("status", result.get("status"));
        data.put("submitTime", result.get("submitTime"));
        return ApiResponse.success(data);
    }



    // 5. 忘记密码
    @PostMapping("/forgot-password")
    public ApiResponse<Map<String, Object>> forgot(@RequestBody Map<String, String> body) {
        Map<String, Object> data = new HashMap<>();
        data.put("resetToken", "reset_token_123456");
        data.put("expiresIn", 3600);
        return ApiResponse.success(data);
    }

    // 6. 重置密码
    @PostMapping("/reset-password")
    public ApiResponse<Void> reset(@Valid @RequestBody ResetPasswordRequest req) {
        return ApiResponse.success(null);
    }

    // 7. 登出
    @PostMapping("/logout")
    public ApiResponse<Void> logout() {
        return ApiResponse.success(null);
    }

    // 8. 获取个人信息
    @GetMapping("/profile")
    public ApiResponse<Map<String, Object>> profile() {
        // 暂时使用固定用户ID，后续可以从JWT token中获取
        Long userId = 1L;
        Map<String, Object> result = authService.getUserProfile(userId);
        if ((int)result.getOrDefault("code", 0) != 0) {
            return ApiResponse.failure((int)result.get("code"), (String)result.get("message"));
        }
        return ApiResponse.success(result);
    }

    // 9. 修改个人信息
    @PutMapping("/profile")
    public ApiResponse<Map<String, Object>> updateProfile(@Valid @RequestBody UpdateProfileRequest req) {
        // 暂时使用固定用户ID，后续可以从JWT token中获取
        Long userId = 1L;
        Map<String, Object> result = authService.updateProfile(userId, req);
        if ((int)result.getOrDefault("code", 0) != 0) {
            return ApiResponse.failure((int)result.get("code"), (String)result.get("message"));
        }
        return ApiResponse.success(result);
    }



    // 10. 修改密码
    @PostMapping("/change-password")
    public ApiResponse<Void> changePassword(@Valid @RequestBody ChangePasswordRequest req) {
        return ApiResponse.success(null);
    }


    
}


